/* 
 * File:   Server.h
 * Author: brady
 *
 * Created on October 17, 2010, 9:15 PM
 */

#ifndef _SERVER_H
#define	_SERVER_H

//#include <stdio.h>
//#include <stdlib.h>
#include <iostream>
#include <netdb.h>
#include <sstream>
//#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <arpa/inet.h>
#include <vector>
#include <fstream>
using namespace std;

class Server {
public:
    
    Server(int port, string socket_type, int ipv);
    void SetSockType(string sock_type);
    void SetIPv(int ipv);
    void SetPort(int server_port);

    //TCP ONLY FUNCTIONS
    bool IsClientConnected(int client);
    void SetMaxClients(int n);
    void Listen();
    int Accept();
    void SendFile(int client, string prefix, string fpath, string suffix);
    void SendFile(int client, string fpath);
    void SendString(int client, string msg);
    void Send(int client, void* data, int length);
    int Receive(int client, void* buffer, int bufferSize);
    int ClientCount();
    void RemoveClient(int client);
    //UDP ONLY FUNCTIONS
    char* ReceiveFromAny();
    void ReplyToLast(const char* msg);
    


private:
    int server_sockfd;
    int* client_sockfds;
    struct sockaddr* client_addresses;
    struct sockaddr_in server_addressV4;
    struct sockaddr_in6 server_addressV6;
    struct sockaddr* server_address;
    int max_clients;
    int socketType;
    int IPv;
    int port;
    struct addrinfo* serverInfo;
    struct addrinfo hints;
    bool bound;

    stringstream bufferstream;

    in_port_t get_in_port(struct sockaddr *sa);

    int GetFreeClient();
    void BuildServer();


};

#endif	/* _SERVER_H */

